﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CGeneral.CheckLogin(Request, Session);

        usernameempty.Attributes["style"] = "display:none";
        passwordcheck.Attributes["style"] = "display:none";
    }

    public void submit(object sender, EventArgs e)
    {
        //usernameempty.Attributes["style"] = "display:none";
        //passwordcheck.Attributes["style"] = "display:none";
        if (string.IsNullOrEmpty(Request.Form["username"]))
            usernameempty.Attributes["style"] = "display:block";
        else if (string.IsNullOrEmpty(Request.Form["password"]))
        {
            passwordcheck.InnerText = "*密码不能为空";
            passwordcheck.Attributes["style"] = "display:block";
        }
        else
        {
            members m = Check.LoginCheck(Request.Form["username"], Request.Form["password"]);
            if (m!=null)
            {
                if (m.gid == 4)
                {
                    CGeneral.GoToErrorPage(Response, "custom", "该用户已被和谐");

                }
                Session["username"] = m.username;
                Session["uid"] = m.uid;
                Session["nickname"] = m.nickname;
                Session["gid"] = m.gid;
                Session["LoginTime"] = DateTime.Now;

                string ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
                MemDisplay.Login(m, ip);
                if(autoLogin.Checked)
                {
                    HttpCookie MyCookie = new HttpCookie("user");
                    MyCookie["username"] = Request.Form["username"];
                    MyCookie["password"] = Request.Form["password"];
                    MyCookie.Expires = DateTime.Today.AddDays(365);
                    Response.Cookies.Add(MyCookie);
                }
                if (Request.QueryString["ref"] != null && Request.QueryString["ref"].IndexOf("Error.aspx")==-1)
                {
                    Response.Redirect(Request.QueryString["ref"]);
                }
                Response.Redirect("Index.aspx");
            }
            else
            {
                passwordcheck.InnerText = "*用户名密码不匹配";
                passwordcheck.Attributes["style"] = "display:block";
            }
        }

    }

    public void regist(object sender, EventArgs e)
    {
        RegisterPart.Attributes["style"] = "display:block";

    }

    public void reg_Click(object sender, EventArgs e)
    {
        string name = Request.Form["reg_username"];
        string pwd = Request.Form["reg_password"];
        string pwd2 = Request.Form["reg_password2"];
        string nick = Request.Form["reg_nickname"];

        if (string.IsNullOrEmpty(name))
        {
            this.regerrormsg.InnerText = "用户名不能为空";
        }
        else if (Check.IsUsernameRepeat(name) == false)
        {
            this.regerrormsg.InnerText = "该用户名已经被注册";
        }
        else if(Check.IsUsernameLegal(name)==false)
        {
            this.regerrormsg.InnerText = "用户名不合法";
        }
        else if (Check.IsPasswordLegal(pwd) == false)
        {
            this.regerrormsg.InnerText = "密码不符合要求";
        }
        else if (pwd != pwd2)
        {
            this.regerrormsg.InnerText = "两次密码输入不符合";
        }
        else if(Check.IsNicknameLegal(nick) == false)
        {
            this.regerrormsg.InnerText = "该昵称不合法";
        }
        else if (Check.IsNicknameRepeat(nick) == false)
        {
            this.regerrormsg.InnerText = "该昵称已经被注册";
        }
        else
        {
            members m = CreatMember(name, pwd, nick);
            SaveData.Regist(m);
            Session["username"] = m.username;
            Session["uid"] = m.uid;
            Session["gid"] = m.gid;
            Session["nickname"] = m.nickname;
            

            /*系统发消息
            CNotice cn = new CNotice();
            cn.fromUid = 1;
            cn.toUid = m.uid;
            cn.isReaded = 0;
            cn.sendtime = DateTime.Now;
            cn.tid = -1;
            cn.pid = -1;
            cn.isReaded = 0;
            cn.messageSend = "欢迎来到AOJ论坛，请和谐发帖";
            cn.address = "index.aspx";
            cn.SendNotice();*/


            CGeneral.GoToErrorPage(Response, "custom", "注功册成功，记得完善个人资料", "3秒后转到论坛首页","Default.aspx");
        }
        
    }

    private members CreatMember(string name,string pwd,string nick)
    {
        members m = new members();
        m.username = name;
        m.password = ConvertData.ToMD5(pwd);
        m.nickname = nick;
        m.gid = 3;
        m.onlinetime = 0;
        m.reg_ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
        m.reg_time = DateTime.Now;
        m.lastlogin_ip = m.reg_ip;
        m.lastlogin_time = m.reg_time;
        m.threadsnum = 0;
        m.replynum = 0;
        m.viewnum = 0;
        m.credits = 0;
        return m;
    }
}